package com.dhh.toodledo.util;

import java.util.Date;

import android.content.SharedPreferences;

import com.dhh.toodledo.exception.InValidAccountException;
import com.dhh.toodledo.model.AccountInfo;

public class AuthUtil {
	private SharedPreferences sp;
	
	public AuthUtil(){}	

	public AuthUtil(SharedPreferences sp){
		this.sp = sp;
	}
	
	public String getKeyValue() throws InValidAccountException{		
		String keyValue = sp.getString("key", null);		
		if(keyValue==null){
			keyValue = generateKey();
		}
		
		//check the key is out of date
		long now = new Date().getTime();
		long update = sp.getLong("keyUpdate", now);
		if((update>0 && !((3*3600*1000)>=(now-update)))){
			keyValue = generateKey();
		}
		
		return keyValue;
	}
	
	private String generateKey() throws InValidAccountException{
		String email = sp.getString("email", null);
		String password = sp.getString("password", null);
		
		if(email==null || password==null){
			throw new InValidAccountException();
		}
		
		String userid = null;
		try {
			userid = HttpUtil.lookupAccount(email, password);
		} catch (Exception e) {
		}
		if(userid==null){
			throw new InValidAccountException();
		}
		
		String token = null;
		try {
			token = HttpUtil.getSessionToken(userid);
		} catch (Exception e) {
		}
		if(token==null){
			throw new InValidAccountException();
		}
		
		String key = HttpUtil.generateKey(password, token);
		if(key==null || !updateAccount(key)){
			throw new InValidAccountException();
		}
		
		return key;
	}
	
	private boolean updateAccount(String key){
		AccountInfo account = null;
		try {
			account = HttpUtil.getAccountInfo(key);
		} catch (Exception e) {
		}
		if(account==null)
			return false;
		SharedPreferences.Editor ac = sp.edit();
		ac.putString("userid", account.getUserid());
		ac.putString("alias", account.getAlias());
		ac.putInt("dateformat", account.getDateformat());
		ac.putInt("timezone", account.getTimezone());
		ac.putInt("hidemonths", account.getHidemonths());
		ac.putInt("hotlistpriority", account.getHotlistpriority());
		ac.putInt("hotlistduedate", account.getHotlistduedate());
		ac.putInt("hotliststatus", account.getHotliststatus());
		ac.putInt("hotliststar", account.getHotliststar());
		ac.putInt("showtabnums", account.getShowtabnums());
		ac.putString("key", key);
		ac.putLong("keyUpdate", new Date().getTime());
		ac.commit();
		return true;
	}
	
	public void login(String email,String password) throws InValidAccountException{
		
		String userid = null;
		try {
			userid = HttpUtil.lookupAccount(email, password);
		} catch (Exception e) {}
		if(userid==null){
			throw new InValidAccountException();
		}
		
		String token = null;
		try {
			token = HttpUtil.getSessionToken(userid);
		} catch (Exception e) {}
		if(token==null){
			throw new InValidAccountException();
		}
		
		String key = HttpUtil.generateKey(password, token);
		if(key==null || !updateAccount(key)){
			throw new InValidAccountException();
		}		
		
		SharedPreferences.Editor ac = sp.edit();
		ac.putString("userid", userid);
		ac.putString("email", email);
		ac.putString("password", password);
		ac.commit();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
